using System;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

//CID 0467 //2009.11.20 //Begin
namespace InfoJetSoft.Service.Worker
{
	class XSNViewFormatNamespaceWorker
	{
		public static void formatNamespace( Manifest manifest, View view )
		{
			foreach( DataObject dataObject in manifest.DataObjectList )
			{
				if( dataObject.WSAdapter != null && dataObject.WSAdapter.Input != null )
				{
					DataInput dataInput = dataObject.WSAdapter.Input;
					if( dataInput.SourceDoc == null )
					{
						dataInput.SourceDoc = DOMUtil.buildDoc( dataInput.SourceContent );
					}
					XSNViewFormatNamespaceWorker.applyNamespace( view, dataInput.SourceDoc );
				}
			}
		}

		private static void applyNamespace( View view, XmlDocument doc )
		{
			foreach( XmlAttribute attr in doc.DocumentElement.Attributes )
			{
				if( attr.NamespaceURI.Equals( JetConstant.URI_XMLNS ) && attr.Prefix != null && attr.Prefix.Equals( "xmlns" ) && attr.LocalName.Length > 0 )
				{
					XmlAttribute sameAttr = view.Doc.DocumentElement.GetAttributeNode( attr.LocalName, JetConstant.URI_XMLNS );
					if( sameAttr == null )
					{
						sameAttr = view.Doc.CreateAttribute( "xmlns", attr.LocalName, JetConstant.URI_XMLNS );
						sameAttr.Value = attr.Value;
						view.Doc.DocumentElement.SetAttributeNode( sameAttr );
					}
				}
			}
		}
	}
}
//CID 0467 //2009.11.20 //End